+<?php
+
+class junk_keyword extends rcube_plugin {
+ public $task = 'mail';
+
+ function init() {
+ #write_log($this->name, "init: task=" . print_r($rcmail->task, true) . " action=" . print_r($rcmail->action, true));
+ $rcmail = rcmail::get_instance();
+ $this->name = get_class($this);
+ if (($rcmail->task == 'mail') && ($rcmail->action == '')) {
+ $this->add_hook('render_page', array($this, 'render_page'));
+ $this->include_script('junk_keyword.js');
+ }
+ $this->add_junk_flags = array
+ ( 'JUNK' => 'Junk'
+ , 'NONJUNK' => 'NonJunk'
+ );
+ $this->register_action('plugin.junk_keyword.set_keywords', array($this, 'set_keywords'));
+ $this->add_hook('messages_list', array($this, 'message_list'));
+ $this->add_texts('localization');
+ }
+ public function render_page($args) {
+ #write_log($this->name, "render_page: args=" . print_r($args, true));
+ $rcmail = rcmail::get_instance();
+ $icon = 'plugins/junk_keyword/' .$this->local_skin_path(). '/junk.png';
+ $junk_keywordicon = html::img(array
+ ( 'alt' => $this->gettext('junk_keyword')
+ , 'id' => 'junk_keyword_menulink'
+ , 'src' => $icon
+ , 'title' => $this->gettext('junk_keyword')));
+ $rcmail->output->add_label('junk_keyword.junk_keyword');
+ $rcmail->output->set_env('junk_keywordicon', $junk_keywordicon);
+ $this->include_stylesheet($this->local_skin_path(). '/junk.css');
+
+ return $args;
+ }
+ public function message_list($args) {
+ #write_log($this->name, "message_list: args=" . print_r($args, true));
+ if (!isset($args['messages']) or !is_array($args['messages']))
+ return $args;
+ foreach($args['messages'] as $message) {
+ #write_log($this->name, "message_list: foreach: begin: message=" . print_r($message, true));
+ $message->list_flags['extra_flags']['junk_keywords'] = array(); # always set extra_flags, needed for javascript later!
+ if (is_array($message->flags)) {
+ $uid = $message->uid;
+ $class = '';
+ foreach ($message->flags as $flagname => $flagvalue) {
+ $flag = is_numeric("$flagvalue") ? $flagname : $flagvalue; // for compatibility with < 0.5.4
+ $flag = strtolower($flag);
+ #write_log($this->name, "message_list: foreach: flag=" . print_r($flag, true));
+ $message->list_flags['extra_flags']['junk'] = false;
+ if (preg_match('/^junk$/', $flag)) {
+ $class = "junk";
+ $message->list_flags['extra_flags']['junk'] = true;
+ }
+ if (preg_match('/^nonjunk$/', $flag)) {
+ $class = "nonjunk";
+ $message->list_flags['extra_flags']['junk'] = false;
+ }
+ }
+ if (!empty($uid))
+ $message->list_cols['junk_keyword'] = '<div id="rcmjunk_keyword'.$uid.'" name="rcmjunk_keyword'.$uid.'" class="'.$class.'"> </div>';
+ }
+ }
+ return $args;
+ }
+ public function set_keywords() {
+ #write_log($this->name, "set_keywords: _GET=" . print_r($_GET, true));
+
+ $rcmail = rcmail::get_instance();
+ $imap = $rcmail->imap;
+ //$cbox = get_input_value('_cur', RCUBE_INPUT_GET);
+ $mbox = get_input_value('_mbox', RCUBE_INPUT_GET);
+ $junk_uids = get_input_value('_junk_uids', RCUBE_INPUT_GET);
+ $junk_uids = array_filter(explode(',', $junk_uids));
+ $nonjunk_uids = get_input_value('_nonjunk_uids', RCUBE_INPUT_GET);
+ $nonjunk_uids = array_filter(explode(',', $nonjunk_uids));
+
+ $imap->conn->flags = array_merge($imap->conn->flags, $this->add_junk_flags);
+
+ if (!is_array($junk_uids) || !is_array($nonjunk_uids))
+ return false;
+ //write_log($this->name, "set_keywords: junk_uids=" . print_r($junk_uids, true));
+ //write_log($this->name, "set_keywords: nonjunk_uids=" . print_r($nonjunk_uids, true));
+
+ $error = "";
+ $imap->conn->error = "";
+ if (!empty($junk_uids)) {
+ if (!$imap->set_flag($junk_uids, 'SEEN', $mbox))
+ $error = "set SEEN: " . $imap->conn->error;
+ if (!$imap->unset_flag($junk_uids, 'NONJUNK', $mbox))
+ $error = "unset NONJUNK: " . $imap->conn->error;
+ if (!$imap->set_flag($junk_uids, 'JUNK', $mbox))
+ $error = "set JUNK: " . $imap->conn->error;
+ }
+ if (!empty($nonjunk_uids)) {
+ if (!$imap->unset_flag($nonjunk_uids, 'JUNK', $mbox))
+ $error = "unset JUNK: " . $imap->conn->error;
+ if (!$imap->set_flag($nonjunk_uids, 'NONJUNK', $mbox))
+ $error = "set NONJUNK: " . $imap->conn->error;
+ }
+ if (empty($error))
+ $rcmail->output->command('display_message', $this->gettext('classification_done'), 'confirmation');
+ else
+ $rcmail->output->command('display_message', $this->gettext('classification_failed'). ': ' . $error, 'error');
+ $this->api->output->send();
+ return empty($error);
+ }
+ }
+?>